<?php
class Reservation_model extends CI_Model
{
	//search lab room
	public function search_lab_room($peoplecount, $comcount, $sw, $hw, $date, $time_start, $time_end)
	{
		$sql = "Select * from room where roomtype=0 and roomstat=1 and roommax>=".$peoplecount." and comcount>=".$comcount ." and sw like '".$sw ."' and hw like '".$hw ."';";
		$query = $this->db->query($sql);
		
		if($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row) 
			{
				$sql1 = "Select * from reservation where roomname='" .$row['roomname'] ."' and usedate like '$date' and resstatus=1";
				$query1 = $this->db->query($sql1);
				
				$hh_arr;
				$hh_DB_arr;
				
				if($query1->num_rows() > 0)
				{
					//check time
					foreach ($query1->result_array() as $row1) {
						$hh_start = substr($time_start,0,2);
						$hh_end = substr($time_end,0,2);
						$hh_DB_start = substr($row1['usetime_start'],0,2);
						$hh_DB_end = substr($row1['usetime_end'],0,2);
						$total_time_day = 8-(($hh_DB_end+0)-($hh_DB_start+0));
						$total_time_use = ($hh_end+0)-($hh_start+0);
						
						$hh_value = $hh_start;
						$hh_DB_value = $hh_DB_start;
						
						//input hh_arr[]
						for($i=0; $i<($hh_end-$hh_start); $i++){
							$hh_arr[$i] = $hh_value;
							$hh_value++;
						}
						
						//input hh_DB_arr[]
						for($i=0; $i<=($hh_DB_end-$hh_DB_start); $i++){
							$hh_DB_arr[$i] = $hh_DB_value;
							$hh_DB_value++;
						}
						
						if($hh_start == $hh_DB_end || $hh_end == $hh_DB_start)
						{
							$room[] = $row;
						}
						else{
						
							$check = true;
							
							//check time
							for($i=0; $i<count($hh_DB_arr); $i++)
							{
								for($j=0; $j<count($hh_arr); $j++)
								{
									if($hh_DB_arr[$i] == $hh_arr[$j])
									{
										$check = false;
										break;
									}	
									else{
										$check = true;
										continue;
									}
								}
								if($check == false){
									break;
								}
								else{
									continue;
								}
							}
							
							if($check == true)
							{
								$room[] = $row;
							}
							
						}
						
					}
				}
				else{
					$room[] = $row;
				}
				
			}
			if(isset($room)){
				return $room;
			}
			
		}
		
	}
	//end search lab room function

	//search meeting room
	public function search_meeting_room($peoplecount, $hw, $date, $time_start, $time_end)
	{
		$sql = "Select * from room where roomtype=1 and roomstat=1 and roommax>=".$peoplecount." and hw like '".$hw ."';";
		$query = $this->db->query($sql);
		
		if($query->num_rows() > 0)
		{
			foreach ($query->result_array() as $row) 
			{
				$sql1 = "Select * from reservation where roomname='" .$row['roomname'] ."' and usedate like '$date' and resstatus=1";
				$query1 = $this->db->query($sql1);
				
				$hh_arr;
				$hh_DB_arr;
				
				if($query1->num_rows() > 0)
				{
					//check time
					foreach ($query1->result_array() as $row1) {
						$hh_start = substr($time_start,0,2);
						$hh_end = substr($time_end,0,2);
						$hh_DB_start = substr($row1['usetime_start'],0,2);
						$hh_DB_end = substr($row1['usetime_end'],0,2);
						$total_time_day = 8-(($hh_DB_end+0)-($hh_DB_start+0));
						$total_time_use = ($hh_end+0)-($hh_start+0);
						
						$hh_value = $hh_start;
						$hh_DB_value = $hh_DB_start;
						
						//input hh_arr[]
						for($i=0; $i<($hh_end-$hh_start); $i++){
							$hh_arr[$i] = $hh_value;
							$hh_value++;
						}
						
						//input hh_DB_arr[]
						for($i=0; $i<=($hh_DB_end-$hh_DB_start); $i++){
							$hh_DB_arr[$i] = $hh_DB_value;
							$hh_DB_value++;
						}
						
						if($hh_start == $hh_DB_end || $hh_end == $hh_DB_start)
						{
							$room[] = $row;
						}
						else{
						
							$check = true;
							
							//check time
							for($i=0; $i<count($hh_DB_arr); $i++)
							{
								for($j=0; $j<count($hh_arr); $j++)
								{
									if($hh_DB_arr[$i] == $hh_arr[$j])
									{
										$check = false;
										break;
									}	
									else{
										$check = true;
										continue;
									}
								}
								if($check == false){
									break;
								}
								else{
									continue;
								}
							}
							
							if($check == true)
							{
								$room[] = $row;
							}
							
						}
						
					}
				}
				else{
					$room[] = $row;
				}
				
			}
			if(isset($room)){
				return $room;
			}
			
		}
		
	}
	//end search meeting room
	
	//check duplicate reserve key
	public function check_duplicate_reservekey($key)
	{
		$array = array('reskey'=>$key);
		$query = $this->db->get_where('reservation', $array);
		
		if($query->num_rows() > 0)
		{
			return true;
		}
		else {
			return false;
		}
	}
	//end check duplicate reserve key
	
	//reserve lab room function
	public function reserve_lab_room($memid, $roomname, $usedate, $usetime_start, $usetime_end, $resdate, $restime, $swuse, $hwuse, $pcount, $comcount, $reskey)
	{
		if($this->session->userdata('psumember') == 1)
		{
			$status = 1;
		}
		else if($this->session->userdata('psumember') == 0)
		{
			$status = 0;
		}
		$data = array(
			'resid' => null,
			'memid' => $memid,
			'roomname' => $roomname,
			'usedate' => $usedate,
			'usetime_start' => $usetime_start,
			'usetime_end' => $usetime_end,
			'resdate' => $resdate,
			'restime' => $restime,
			'swuse' => $swuse,
			'hwuse' => $hwuse,
			'pcount' => $pcount,
			'comcount' => $comcount,
			'resstatus' => $status,
			'reskey' => $reskey
		);
		
		$query = $this->db->insert('reservation', $data);
		
		if($query == true){
			return true;
		} else {
			return false;
		}
	}
	//end reserve lab room function
	
	//reserve meeting room function
	public function reserve_meeting_room($memid, $roomname, $usedate, $usetime_start, $usetime_end, $resdate, $restime, $hwuse, $pcount, $reskey)
	{
		if($this->session->userdata('psumember') == 1)
		{
			$status = 1;
		}
		else if($this->session->userdata('psumember') == 0)
		{
			$status = 0;
		}
		$data = array(
			'resid' => null,
			'memid' => $memid,
			'roomname' => $roomname,
			'usedate' => $usedate,
			'usetime_start' => $usetime_start,
			'usetime_end' => $usetime_end,
			'resdate' => $resdate,
			'restime' => $restime,
			'hwuse' => $hwuse,
			'pcount' => $pcount,
			'resstatus' => $status,
			'reskey' => $reskey
		);
		
		$query = $this->db->insert('reservation', $data);
		
		if($query == true){
			return true;
		} else {
			return false;
		}
	}
	//end reserve meeting room function
	
	//check time reservation function
	public function check_time($room, $date, $time_start, $time_end)
	{
		$sql = "Select * from reservation where roomname='" .$room ."' and usedate like '$date' and resstatus=1";
		$query = $this->db->query($sql);

		//declare variable
		$hh_arr;
		$hh_DB_arr;
				
		if($query->num_rows() > 0)
		{
			$can_reserve = false;
			
			//check time
			foreach ($query->result_array() as $row) {
				$hh_start = substr($time_start,0,2);
				$hh_end = substr($time_end,0,2);
				$hh_DB_start = substr($row['usetime_start'],0,2);
				$hh_DB_end = substr($row['usetime_end'],0,2);
				$total_time_day = 8-(($hh_DB_end+0)-($hh_DB_start+0));
				$total_time_use = ($hh_end+0)-($hh_start+0);
						
				$hh_value = $hh_start;
				$hh_DB_value = $hh_DB_start;
						
				//input hh_arr[]
				for($i=0; $i<($hh_end-$hh_start); $i++){
					$hh_arr[$i] = $hh_value;
					$hh_value++;
				}
						
				//input hh_DB_arr[]
				for($i=0; $i<=($hh_DB_end-$hh_DB_start); $i++){
					$hh_DB_arr[$i] = $hh_DB_value;
					$hh_DB_value++;
				}
						
				if($hh_start == $hh_DB_end || $hh_end == $hh_DB_start)
				{
					$can_reserve = true;
				}
				else{
					$check = true;
							
					//check time
					for($i=0; $i<count($hh_DB_arr); $i++)
					{
						for($j=0; $j<count($hh_arr); $j++)
						{
							if($hh_DB_arr[$i] == $hh_arr[$j])
							{
								$check = false;
								break;
							}	
							else{
								$check = true;
								continue;
							}
						}
						if($check == false){
							break;
						}
						else{
							continue;
						}
					}
							
					if($check == true)
					{
						$can_reserve = true;
					}			
				}		
			}
		}
		else{
			$can_reserve = true;
		}
				
		return $can_reserve;
	}
	//end check time reservation function
	
}